// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#ifndef MGCBEZIERRECTANGLE2_H
#define MGCBEZIERRECTANGLE2_H

#include "MgcBezierRectangle.h"
#include "MgcColor.h"
#include "MgcVector2.h"
#include "MgcVector3.h"


class MgcBezierRectangle2 : public MgcBezierRectangle
{
    MgcDeclareRTTI;
    MgcDeclareStream;

public:
    // Construction.  MgcBezierRectangle2 accepts responsibility for deleting
    // the input array.
    MgcBezierRectangle2 (unsigned int* auiIndex);

    // tessellation
    virtual void Tessellate (unsigned int uiLevel,
        const MgcVector3* akCtrlPoint, const MgcColor* akCtrlColor,
        const MgcVector2* akCtrlTexture, MgcTriMesh* pkMesh,
        unsigned int& ruiVertexStart, unsigned int& ruiTriangleStart);

protected:
    MgcBezierRectangle2 ();

    // precomputation
    class BlockParameters
    {
    public:
        unsigned int m_uiI00, m_uiI01, m_uiI10, m_uiI11;

        // Vss(0,0), Vss(0,1), Vss(1,0) = Vss(0,0), Vss(1,1) = Vss(0,1)
        MgcVector3 m_akXss[2];
        MgcColor m_akCss[2];
        MgcVector2 m_akTss[2];

        // Vtt(0,0), Vtt(1,0), Vtt(0,1) = Vtt(0,0), Vtt(1,1) = Vtt(1,0)
        MgcVector3 m_akXtt[2];
        MgcColor m_akCtt[2];
        MgcVector2 m_akTtt[2];

        // Xsst(0,0), Xsst(0,1), Xsst(1,0) = Xsst(0,0), Xsst(1,1) = Xsst(0,1)
        MgcVector3 m_akXsst[2];

        // Xstt(0,0), Xstt(1,0), Xstt(0,1) = Xstt(0,0), Xstt(1,1) = Xstt(1,0)
        MgcVector3 m_akXstt[2];
    };

    void InitializePoints (const MgcVector3* akCtrlPoint, MgcVector3& rkXsstt,
        BlockParameters& rkBP);

    void InitializeNormals (const MgcVector3* akCtrlPoint,
        BlockParameters& rkBP);

    void InitializeColors (const MgcColor* akCtrlColor, MgcColor& rkCsstt,
        BlockParameters& rkBP);

    void InitializeTextures (const MgcVector2* akCtrlTexture,
        MgcVector2& rkTsstt, BlockParameters& rkBP);

    // tessellation by recursive subdivision
    void Subdivide (unsigned int uiLevel, MgcReal fDSqr, MgcVector3* akX,
        MgcVector3* akXs, MgcVector3* akXt, MgcColor* akColor,
        MgcVector2* akTexture, MgcVector3& rkXsstt, MgcColor& rkCsstt,
        MgcVector2& rkTsstt, BlockParameters& rkBP);
};

MgcSmartPointer(MgcBezierRectangle2);
MgcRegisterStream(MgcBezierRectangle2);

#endif
